﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using SYH.Config;
using System.Data;

namespace SYH.User.DAL
{
  public   class UserArea
    {
        /// <summary>
        /// 获得会员地区信息Id  省市区的Id  “,”分隔
        /// </summary>
        /// <param name="UserId"></param>
        /// <returns></returns>
        public string GetUserAreaId(int UserId)
        {
            SqlDataReader sdr = Common.SqlHelper.ExecuteReader(WebConfig.SqlConnString,
                           CommandType.StoredProcedure,
                           "User_Area_GetUserAreaId",
                           new SqlParameter("@UserId", UserId));
            if (sdr.HasRows)
            {
                string area = string.Empty;
                while (sdr.Read())
                {
                    area += sdr.GetInt32(sdr.GetOrdinal("DistrictId")) + ",";
                }
                sdr.Close();
                return area.Substring(0, area.Length - 1);
            }
            sdr.Close();
            return "0,0,0";
        }

        /// <summary>
        /// 用户地址
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public string GetUserArea(int id)
        {
            string UserArea = string.Empty;

            StringBuilder strsql = new StringBuilder();
            strsql.Append(" select Name from public_district where Id in ");
            strsql.AppendFormat(" (select DistrictId from user_area where UserId={0} and level in(1,2))", id);
            DataTable dt = Common.SqlHelper.Query(WebConfig.SqlConnString, strsql.ToString()).Tables[0];

            if (dt.Rows.Count > 0)
            {
                for (int u = 0; u < dt.Rows.Count; u++)
                {
                    UserArea += dt.Rows[u][0].ToString();
                }
            }
            return UserArea;
        }
    }
}
